// This File is part of the "nyaqt" Project
// 
// Copyright  2009 sometmes@gmail.com
// All rights reserved
// 
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.



using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Security.Permissions;
using System.Security;
using System.Configuration;
using System.Reflection;
using System.Data;

namespace nyaqt
{
    public sealed class NyaqtProviderFactory
    {
        Pre20ProviderFactoriesElement _pre20Provider;

        DbProviderFactory _post20Provider;

        public DbProviderFactory DbProviderFactory
        {
            get { return _post20Provider; }
            set { _post20Provider = value; }
        }

        public NyaqtProviderFactory(string invariantName, bool supportsMicrosoftProviderFactory)
        {
            if (supportsMicrosoftProviderFactory)
            {
                _post20Provider = DbProviderFactories.GetFactory(invariantName);
            }
            else
            {
                foreach (Pre20ProviderFactoriesElement provider in My.Settings.Pre20ProviderFactories.Pre20Providers)
                {
                    if (provider.InvariantName == invariantName)
                    {
                        _pre20Provider = provider;
                        break;
                    }
                }
            }
        }

        public IDbConnection CreateConnection()
        {
            if (_post20Provider != null)
            {
                return _post20Provider.CreateConnection();
            }
            if (_pre20Provider != null)
            {
                Type t = Type.GetType(_pre20Provider.ConnectionQualifiedName);
                IDbConnection cn = Activator.CreateInstance(t) as IDbConnection;
                //ConstructorInfo constr = t.GetConstructor(Type.EmptyTypes);
                //DbConnection cn = constr.Invoke(null) as ;
                return cn;
            }
            return null;
        }

        // Properties
        public bool CanCreateDataSourceEnumerator
        {
            get
            {
                if (_pre20Provider != null)
                    return false;
                if (_post20Provider != null)
                    return _post20Provider.CanCreateDataSourceEnumerator;
                else
                    return false;
            }
        }
    }


}
































